package com.liyuanjinglyj.javacarddemo.widget.widget;

import com.liyuanjinglyj.javacarddemo.ResourceTable;

import com.liyuanjinglyj.javacarddemo.adapter.InfoItemListProvider;
import com.liyuanjinglyj.javacarddemo.model.InfoItem;
import com.liyuanjinglyj.javacarddemo.slice.WebViewAbilitySlice;
import com.liyuanjinglyj.javacarddemo.utils.ImageReadUtil;
import com.liyuanjinglyj.javacarddemo.utils.LYJUtils;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import ohos.utils.zson.ZSONObject;
import org.devio.hi.json.HiJson;

import java.util.ArrayList;
import java.util.List;

/**
 * Fourth function ability slice.鸿蒙开发
 */
public class FunctionHarmonySlice extends AbilitySlice {
    HiLogLabel label=new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
    private static final String PARAMS = "params";
    private static final String MESSAGE = "message";
    private String url="https://harmony-1300376177.cos.ap-shanghai.myqcloud.com/harmony_item.json";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LYJUtils.getListContainer(url,this));
    }
}
/***
 ListContainer listContainer=new ListContainer(null);
 listContainer.setLayoutConfig(
 new StackLayout.LayoutConfig(
 ComponentContainer.LayoutConfig.MATCH_PARENT,
 ComponentContainer.LayoutConfig.MATCH_PARENT
 ));
 ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
@Override
public void onFailure(int code, String errorMessage) {
//http访问出错了，此部分内容在主线程中工作;
//可以更新UI等操作,请不要执行阻塞操作。
HiLog.error(label,String.valueOf(code));
}
@Override
public void onResponse(String response) {
//http访问成功，此部分内容在主线程中工作;
//可以更新UI等操作，但请不要执行阻塞操作。
HiLog.error(label,response);
HiJson hiJson = new HiJson(response);
int counts=hiJson.get("news_item").count();
List<InfoItem> infoItemList=new ArrayList<>();
for(int i=0;i<counts;i++){
InfoItem infoItem=new InfoItem();
infoItem.setTitle(hiJson.get("news_item").get(i).value("title"));
infoItem.setDigest(hiJson.get("news_item").get(i).value("digest"));
infoItem.setUrl(hiJson.get("news_item").get(i).value("url"));
infoItem.setThumb_url(hiJson.get("news_item").get(i).value("thumb_url"));
infoItemList.add(infoItem);
}
HiLog.error(label,String.valueOf(infoItemList.size()));
InfoItemListProvider infoItemListProvider=new InfoItemListProvider(infoItemList,FunctionFavoritesSlice.this);
listContainer.setItemProvider(infoItemListProvider);
listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
@Override
public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
Intent intent=new Intent();
intent.setParam("url",infoItemList.get(i).getUrl());
present(new WebViewAbilitySlice(),intent);
}
});
}
});
 */

/**
 * 读取网络数据的耗时操作在TaskDispatcher分发的单独线程中处理，
 * 否则会报 "NetworkOnMainThreadException"异常
 */
/***
 * TaskDispatcher refreshUITask = createParallelTaskDispatcher("", TaskPriority.DEFAULT);
 *         refreshUITask.syncDispatch(()->{
 *             PixelMap pixelMap = ImageReadUtil.createPixelMap("https://images-1300376177.cos.ap-shanghai.myqcloud.com/homepage1.jpg");
 *
 *             //Image组件填充位图数据，ui界面更新
 *             Image image = (Image) component.findComponentById(ResourceTable.Id_image_favorites);
 *             image.setPixelMap(pixelMap);
 *             pixelMap.release();
 *         });
 */
